﻿using MySql.Data.MySqlClient;
using System;
using System.Configuration;
using System.Windows.Forms;

namespace TubeBurstAnalysis
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
            skinEngine1.SkinFile = System.Environment.CurrentDirectory + "\\Skins\\MSN.ssk";  //设置皮肤,MSN、Wave推荐
            textBox2.PasswordChar = '*';
            StartPosition = FormStartPosition.CenterScreen;//窗体居中
            Show();
            textBox1.Focus();
        }


        private void button1_Click(object sender, EventArgs e)
        {


            //登录
            string userName = textBox1.Text;
            string userPassword = textBox2.Text;

            //内置登录账号密码（以防服务器停止服务无法工作）
            if (userName.Equals("root") || userPassword.Equals("root"))
            {
                MessageBox.Show("登录成功！");
                //返回登录成功
                DialogResult = DialogResult.OK;
                Close();
                return;
            }

            if (userName.Equals("") || userPassword.Equals(""))
            {
                MessageBox.Show("用户名或密码不能为空！");
            }
            else
            {
                string strcon = ConfigurationManager.ConnectionStrings["connString"].ToString();
                MySqlConnection con = new MySqlConnection(strcon);
                try
                {
                    //2. 打开数据库
                    con.Open();
                    //3. sql语句
                    string sqlSel = "select count(*) from user where user_name = '" + userName +
                        "' and password = '" + userPassword + "'";
                    MySqlCommand com = new MySqlCommand(sqlSel, con);
                    //4.判断executeScalar方法返回的参数是否大于0，大于0表示查找有数据
                    if (Convert.ToInt32(com.ExecuteScalar()) > 0)
                    {
                        MessageBox.Show("登录成功！");
                        //返回登录成功
                        DialogResult = DialogResult.OK;
                        Close();

                    }

                    //用户名和密码验证错误，提示错误。
                    else
                    {
                        MessageBox.Show("用户名或密码错误！");
                    }
                }
                catch (Exception ex)
                {
                    {
                        MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
                    }

                }
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            FormRegister formRegister = new FormRegister();
            formRegister.Show();
        }

    }
}
